package com.tanhua.autoconfig.template;

import com.baidu.aip.face.AipFace;
import com.tanhua.autoconfig.properties.ApiFaceProperties;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/11/27 19:16
 */
public class ApiFaceTemplate {

    private ApiFaceProperties apiFaceProperties;

    public ApiFaceTemplate(ApiFaceProperties apiFaceProperties){
       this.apiFaceProperties = apiFaceProperties;
    }


    /**
     * 人脸检测
     * @param image
     * @return
     */
    public boolean faceCheck(String image){

        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");
        // 初始化一个AipFace
        AipFace client = new AipFace(apiFaceProperties.getAppid(),
                apiFaceProperties.getApiKey(), apiFaceProperties.getSecretKey());
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        // 调用接口
        String imageType = "URL";
        // 人脸检测
        JSONObject res = client.detect(image, imageType, options);
        System.out.println(res.toString(2));
        Integer error_code = (Integer) res.get("error_code");
        return error_code == 0;
    }
}
